<template>
  <a-modal
    title="创建商户组别限额管理"
    v-model="show"
    :maskClosable="false"
    :width="1050"
    placement="right"
    :closable="true"
    @cancel="onClose"
    :footer="null"
    :visiable="bankLevelManagerAddVisiable"
  >
    <a-form :form="form">
      <a-row>新增限额</a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="商户组别"
                       v-bind="formItemLayout">
            <a-select
              show-search
              placeholder="请输入代码或名称"
              option-filter-prop="children"
              style="width: 334px"
              :filter-option="filterOption"
              @focus="getMerchantTypeGroups"
              @search="getMerchantTypeGroups"
              @change="handleInstChange"
              v-decorator="['grpNo',{rules: [{ required: true, message: '必填字段'}]}]"
            >
              <a-select-option v-for="(d,index) in merchantTypeGroup" :key="d.id">
                {{d.name}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item
            label="商户MCC"
            :labelCol="{span: 5}"
            :wrapperCol="{span: 18, offset: 1}"
            v-bind="formItemLayout">
            <a-select
              show-search
              placeholder="请输入代码或名称"
              option-filter-prop="children"
              style="width: 100%"
              :filter-option="filterOption"
              @focus="getMerchantTypes"
              @search="getMerchantTypes"
              @change="handleInstChangeMCC"
              v-decorator="['industryType',{rules: [{ required: true, message: '必填字段'}]}]"
            >
              <a-select-option v-for="(d,index) in merchantType" :key="d.industryType">
                {{d.industryType}}-{{d.industryName}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="日累计限额"
                       v-bind="formItemLayout">
            <a-input v-decorator="['dayLimitAmt']"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label='月累计限额'
                       v-bind="formItemLayout">
            <a-input v-decorator="['monthLimitAmt']"/>
          </a-form-item>
        </a-col>
      </a-row>

      <a-row>借记限额</a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="单笔借记限额"
                       v-bind="formItemLayout">
            <a-input v-decorator="['singleLimitLoanMaxAmt']"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label='日累计借记限额'
                       v-bind="formItemLayout">
            <a-input v-decorator="['dayLimitLoanAmt']"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="月累计借记限额"
                       v-bind="formItemLayout">
            <a-input v-decorator="['monthLimitLoanAmt']"/>
          </a-form-item>
        </a-col>
      </a-row>

      <a-row>贷记限额</a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="单笔贷记限额"
                       v-bind="formItemLayout">
            <a-input v-decorator="['singleLimitCreditMaxAmt']"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label='日累计贷记限额'
                       v-bind="formItemLayout">
            <a-input v-decorator="['dayLimitCreditAmt']"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="月累计贷记限额"
                       v-bind="formItemLayout">
            <a-input v-decorator="['monthLimitCreditAmt']"/>
          </a-form-item>
        </a-col>
      </a-row>

      <br>

    </a-form>

    <div class="drawer-bootom-button">
      <a-popconfirm title="确定放弃编辑？" @confirm="onClose" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-modal>

</template>
<script>
  const formItemLayout = {
    labelCol: {span: 8},
    wrapperCol: {span: 16}
  }
  export default {
    name: 'BankLevelManagerAdd',

    props: {
      bankLevelManagerAddVisiable: {
        default: false
      },
      instId: '',
    },
    data() {
      return {
        data: [],
        grpNoData: [],
        mchntNoLevelInfo: {
          visiable: false
        },
        queryParams: {
          instId: '',
          grpNo: '',
          industryType: '',
        },
        merchantTypeGroup: [], // 商户组别下拉框数据
        merchantType: [], // 商户类别下拉框数据
        loading: false,
        formItemLayout,
        form: this.$form.createForm(this),
        validateStatus: '',
        // firstValue: {},
        info: {
          infoName: '',
          remark: '',
          menuId: ''
        }
      }
    },
    computed: {
      show: {
        get: function () {
          return this.bankLevelManagerAddVisiable
        },
        set: function () {
        }
      }
    },
    mounted() {

    },

    methods: {

      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },

      getMoment(value) {
        return this.$moment(value).format('YYYY-MM-DD HH:MM:SS.MS')
      },

      async debounceSearch({value, url, params}) {

        try {
          this.inputFetching = true
          const res = await this.$get(url, params)
          this.inputFetching = false
          if (res.status === 200) {
            return res
          }
        } catch (e) {
          this.inputFetching = false
          console.error(e)
        }
      },
      async getMerchantTypeGroups (value = '') {
        this.merchantTypeGroup = [];
        this.queryParams.grpNo=="";
        const res = await this.debounceSearch({
          value,
          url: '/admin/mcmp/bMchntBaseInfo/unionMerGrpSelect2List',
          params: {q: encodeURIComponent(value + '&' + this.instId)},
        })
        this.merchantTypeGroup = res.data.rows.map(i => {
          i.name = i.grpNo + ' - ' + i.grpDesc
          i.value = i.grpNo
          return i
        })
      },
      async getMerchantTypes (value = '') {
        if (!this.queryParams.grpNo){
          this.$message.error("请先选择商户组别")
          return
        }
        this.merchantType = [];
        const res = await this.debounceSearch({
          value,
          url: '/admin/quota/bankLevelManager/mchntFormalSelect2List',
          params: {q: encodeURIComponent(value + '&' + this.instId + '&' + this.queryParams.grpNo)},
        })
        console.log(res.data);
        this.merchantType = res.data;
      },
      handleInstChange(value) {
        this.queryParams.instId=this.instId;
          this.queryParams.grpNo=value;
        this.queryParams.industryType=null;
      },
      handleInstChangeMCC(value) {
        this.queryParams.instId=this.instId;
          this.queryParams.industryType=value;
      },
      reset() {
        this.loading = false
        this.form.resetFields()
      },
      onClose() {
        this.reset()
        this.$emit('close')
      },
      handleSubmit() {
        this.form.validateFields((err, values) => {
          if (!err) {
            this.setInfoFields()
            this.loading = true
            this.info.instId = this.instId
            this.info.optFlag = 'add'
            this.info.riskOccurArea = this.info.termAddrPrvnc + '/' + this.info.termAddrCity
            this.info.validDate = this.getMoment(this.info.validDate)
            // console.log(this.info)
            this.$post('/admin/quota/bankLevelManager/saveMerMccQuota', {
              ...this.info
            }).then((r) => {

              if (r.data.code!="100000"){
                this.loading = false
              this.$message.error(r.data.message)
                }else {
                this.reset();
                this.$emit('success')               }
            }).catch(() => {
              this.loading = false
            })
          }
        })

      },

      setInfoFields() {
        let values = this.form.getFieldsValue(['grpNo', 'industryType', 'dayLimitAmt', 'monthLimitAmt', 'singleLimitLoanMaxAmt',
          'dayLimitLoanAmt', 'monthLimitLoanAmt', 'singleLimitCreditMaxAmt', 'dayLimitCreditAmt', 'monthLimitCreditAmt' ])
        if (typeof values !== 'undefined') {
          Object.keys(values).forEach(_key => {
            this.info[_key] = values[_key]
          })
        }
      },


    },

  }
</script>

